package com.sunlee.common.core.exception;

import com.sunlee.common.core.enums.ErrorCodeEnum;
import lombok.Data;
import lombok.EqualsAndHashCode;

/**
 * 业务异常，该异常不会记录到数据库
 * 建议传入Throwable，错误信息会带有带有Caused by
 *
 * @author sunlee
 */
@Data
@EqualsAndHashCode(callSuper = true)
public class BusinessException extends RuntimeException {

    private final int code;
    private final String msg;


    public BusinessException(int code) {
        this.code = code;
        this.msg = ErrorCodeEnum.msgOfCode(code);
    }

    public BusinessException(String msg) {
        super(msg);
        this.code = ErrorCodeEnum.SERVER_INTERNAL_ERROR.getCode();
        this.msg = msg;
    }

    public BusinessException(ErrorCodeEnum errEnum) {
        super(errEnum.getMsg());
        this.code = errEnum.getCode();
        this.msg = errEnum.getMsg();
    }

    public BusinessException(int code, Throwable e) {
        super(e);
        this.code = code;
        this.msg = ErrorCodeEnum.msgOfCode(code);
    }

    public BusinessException(String msg, Throwable e) {
        super(e);
        this.code = ErrorCodeEnum.SERVER_INTERNAL_ERROR.getCode();
        this.msg = msg;
    }

    public BusinessException(ErrorCodeEnum errEnum, Throwable e) {
        super(e);
        this.code = errEnum.getCode();
        this.msg = errEnum.getMsg();
    }
}